package com.cgbl.cg.ui.main

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.FragmentManager
import com.cgbl.cg.R
import com.cgbl.cg.adapter.MessageAdapter
import com.cgbl.cg.databinding.AppFragmentMessageBinding
import com.cgbl.cg.databinding.AppItemMessageBinding
import com.cgbl.cg.im.viewholder.CustomP2PVIewHolder
import com.cgbl.cg.im.viewholder.CustomTeamViewHolder
import com.cgbl.cg.vm.main.MessageFragmentViewModel
import com.netease.yunxin.kit.common.ui.viewholder.BaseViewHolder
import com.netease.yunxin.kit.conversationkit.ui.ConversationKitClient
import com.netease.yunxin.kit.conversationkit.ui.ConversationUIConfig
import com.netease.yunxin.kit.conversationkit.ui.common.ConversationConstant
import com.netease.yunxin.kit.conversationkit.ui.databinding.TeamViewHolderLayoutBinding
import com.netease.yunxin.kit.conversationkit.ui.model.ConversationBean
import com.netease.yunxin.kit.conversationkit.ui.page.ConversationFragment
import com.netease.yunxin.kit.conversationkit.ui.page.DefaultViewHolderFactory
import com.netease.yunxin.kit.conversationkit.ui.view.P2PViewHolder
import com.netease.yunxin.kit.conversationkit.ui.view.TeamViewHolder
import com.robot.base.base.activity.GLBaseFragment
import me.goldze.mvvmhabit.BR


class MessageFragment : GLBaseFragment<AppFragmentMessageBinding, MessageFragmentViewModel>() {

    override fun initContentView(
        inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_message

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initTiterBar()
        initView()
    }

    private fun initView() {
//        messageAdapter = MessageAdapter()
//        binding.recyclerView.apply {
//            layoutManager = LinearLayoutManager(context)
//            adapter = messageAdapter
//        }
//        messageAdapter.setNewInstance(mutableListOf("", ""))
        val fragmentManager: FragmentManager? = fragmentManager
        //创建ConversationFragment
        //创建ConversationFragment
        val fragment = ConversationFragment()
        //将ConversationFragment加载到Activity中
        //将ConversationFragment加载到Activity中
        fragmentManager?.beginTransaction()?.add(R.id.conversation_container, fragment)?.commit()
        val config = ConversationUIConfig()
        //2、设置TitleBar不可见
        config.showTitleBar = false;

        config.conversationFactory = object : DefaultViewHolderFactory() {
            override fun createViewHolder(
                parent: ViewGroup, viewType: Int
            ): BaseViewHolder<ConversationBean>? {
                if (viewType == ConversationConstant.ViewType.CHAT_VIEW) {
                    return CustomP2PVIewHolder(
                        AppItemMessageBinding.inflate(
                            LayoutInflater.from(parent.context), parent, false
                        )
                    )
                } else if (viewType == ConversationConstant.ViewType.TEAM_VIEW) {
                    return CustomTeamViewHolder(
                        AppItemMessageBinding.inflate(
                            LayoutInflater.from(parent.context), parent, false
                        )
                    )
                }
                return null
            }
        }
        //3、将自定义事件设置到ConversationKitClient即可
        ConversationKitClient.setConversationUIConfig(config);
    }

    private fun initTiterBar() {
        val titleView = view?.findViewById<TextView>(R.id.tv_title)
        val backView = view?.findViewById<ImageView>(R.id.iv_back)
        titleView?.text = "消息"
        backView?.visibility = View.GONE
    }
}